<?php

declare (strict_types = 1);

namespace app\shop31\controller;

use think\facade\Cache;
use \think\facade\View;
use \think\facade\Db;
use \think\facade\Request;
class Good{
    public function good_list()
    {

        $param=Request::param();

        View::assign($param);

        $where=[];

        if (isset($param['cid']) && !empty($param['cid'])) $where['cid']=$param['cid'];
        if (isset($param['name']) && !empty($param['name'])) $where['name']=$param['name'];
        if (isset($param['is_down']) && ($param['is_down']) <> '') $where['is_down']=$param['is_down'];
        if (isset($param['is_show']) && ($param['is_show']) <> '') $where['is_show']=$param['is_show'];
        if (isset($param['is_hot']) &&  ($param['is_hot'])  <> '') $where['is_hot']=$param['is_hot'];
        if (isset($param['price']) && !empty($param['price'])) $where['price']=$param['price'];
        if (isset($param['time']) && !empty($param['time'])) $where['time']=$param['time'];



        $get = \think\facade\Request::get();
        View::assign($get);


        $list=\app\shop31\model\Category::getCategory();
        View::assign('list',$list);

        $res = \app\shop31\model\Goods::getGoodsList($where,$param);
        View::assign('res',$res);

//助手函数  -  渲染视图
        return view();
    }

    public function good_add()
    {
        //助手函数  -  渲染视图
        return view();
    }

    public function cate()
    {
        $id=\think\facade\Request::param('id',0);
        $sql =  \think\facade\Db::name('category')
            ->where("parentid","=",$id)
            ->order("sort")
            ->select()
            ->toArray();
//
//        foreach ($sql as &$v){
//            $v['list']=Db::name('category')
//                ->where("parentid","=",$v['id'])
//                ->count();
//        }
//
        //缓存 has set get
        //参数 set (变量名,变量,时间)
        //判断缓存是否设置
        if(Cache::has('cate')){
            //获取缓存信息
            $arr=Cache::get('cate');
        }else{
            $arr=Db::name('category')
                ->distinct(true)
                ->column('parentid');

            Cache::set('cate',$arr,10);
        }

        echo "<pre>";
        print_r($arr);
        echo "</pre>";

        foreach ($sql as &$v){
            //判断当前父类id
            if (in_array($v['id'],$arr)){
                $v['list']=1;
            }else{
                $v['list']=0;
            }
        }
        View::assign('sql',$sql);



        return view();
    }

        public function cate_c()
    {
        $get=\think\facade\Request::get();
        echo "<pre>";
        print_r($get);
        echo "</pre>";

        echo $id=$get['id'];

        $sql3 = Db::name('category')->where("parentid",'=',"$id")->select();

        View::assign('sql3',$sql3);
        $sql =  Db::name('category')->where("parentid=0")->select();
        View::assign('sql',$sql);
        return view();

    }



}